////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include "QuadTreeOccupantHandler.hpp"

#include <SmegLight/Light/LightSystem.hpp>
#include <SmegLight\QuadTree\QuadTreeOccupant.hpp>

#include "../../tinyxml2/tinyxml2.h"
#include "../../Canvas/SmegEditorCanvas.hpp"

////////////////////////////////////////////////////////////
/// Constructor
////////////////////////////////////////////////////////////
QuadTreeOccupantHandler::QuadTreeOccupantHandler() :
    BaseObjectHandler()
{
}

////////////////////////////////////////////////////////////
/// Copy Constructor
////////////////////////////////////////////////////////////
QuadTreeOccupantHandler::QuadTreeOccupantHandler( const QuadTreeOccupantHandler& _Copy ) :
    BaseObjectHandler( _Copy )
{
}

////////////////////////////////////////////////////////////
/// Init the handler and setup the properties
/// BaseObjectHandler::InitFromGrid need to be called AFTER the specialized class
////////////////////////////////////////////////////////////
bool QuadTreeOccupantHandler::InitFromGrid( wxPropertyGrid* _pPropertyGrid ) {
    // Static
    wxPGProperty* pCurProp = _pPropertyGrid->GetProperty("Is Static");
    wxAny staticValue = pCurProp->GetValue();

    smeg::light::LightSystem::GetInstance()->SetStatic( static_cast< smeg::light::QuadTreeOccupant* >( m_pObject ), staticValue.As<bool>() );

    return BaseObjectHandler::InitFromGrid( _pPropertyGrid );
}
    
////////////////////////////////////////////////////////////
/// Init the handler from an XMLElement
/// BaseObjectHandler::InitFromXML need to be called AFTER the specialized class
////////////////////////////////////////////////////////////
bool QuadTreeOccupantHandler::InitFromXML( tinyxml2::XMLElement* _pElement, const wxString& _ProjectPath ) {
    // Get the infos
    tinyxml2::XMLElement* paramElem = _pElement->FirstChildElement( "Param" );

    smeg::light::QuadTreeOccupant* quadOccObj = static_cast< smeg::light::QuadTreeOccupant* >( m_pObject );

    if ( BaseObjectHandler::InitFromXML( _pElement, _ProjectPath ) ) {
        while ( paramElem ) {
            wxString paramName = paramElem->Attribute( "name" );

            if ( "Is Static" == paramName ) {
                // Is Static
                smeg::light::LightSystem::GetInstance()->SetStatic( quadOccObj, paramElem->BoolAttribute( "value" ) );
            }

            paramElem = paramElem->NextSiblingElement( "Param" );
        }
    } else {
        return false;
    }

    return true;
}
    
////////////////////////////////////////////////////////////
/// Return the value of a property
////////////////////////////////////////////////////////////
wxString QuadTreeOccupantHandler::GetValueAsString( const wxString& _PropertyName ) const {
    smeg::light::QuadTreeOccupant * quadOccObj = static_cast< smeg::light::QuadTreeOccupant* >( m_pObject );

    if ( "Is Static" == _PropertyName ) {
        return ( quadOccObj->IsStatic() ? "true" : "false" );
    } else {
        return BaseObjectHandler::GetValueAsString( _PropertyName );
    }
}
    
////////////////////////////////////////////////////////////
/// Handle the change of a property and apply it to the object
////////////////////////////////////////////////////////////
bool QuadTreeOccupantHandler::OnPropertyChanged( wxPropertyGrid* _pPropertyGrid, wxPGProperty* _pChangedProperty ) {
    if ( BaseObjectHandler::OnPropertyChanged( _pPropertyGrid, _pChangedProperty ) ) {
        return true;
    }

    smeg::light::QuadTreeOccupant * quadOccObj = static_cast< smeg::light::QuadTreeOccupant* >( m_pObject );
    
    // Handle changes in values, as needed
    const wxString& propName(_pChangedProperty->GetName());
    if ( "Is Static" == propName ) {
        wxAny staticValue = _pChangedProperty->GetValue();
        smeg::light::LightSystem::GetInstance()->SetStatic( quadOccObj, staticValue.As<bool>() );
    } else {
        return false;
    }

    return true;
}
   
////////////////////////////////////////////////////////////
/// Create an XML string with the value of the object
////////////////////////////////////////////////////////////
wxString QuadTreeOccupantHandler::InnerXMLString( const wxString& _ProjectPath, const wxString& _Tab ) const {
    smeg::light::QuadTreeOccupant * quadOccObj = static_cast< smeg::light::QuadTreeOccupant* >( m_pObject );

    return BaseObjectHandler::InnerXMLString( _ProjectPath, _Tab ) +
        _Tab + wxT( "<Param name=\"Is Static\" value=\"" ) + (quadOccObj->IsStatic() ? wxT( "true" ) : wxT( "false" ) ) + wxT( "\" />\n" );
}